import java.util.*;
import java.io.*;
import java.text.*;

public class EventReader
{
    private static ArrayList<Event> events = new ArrayList<Event>();
    private final static String S = File.separator;
    
    public static ArrayList<Event> getEvents()
    {
        return events;
    }
    
    public static void readEvents()
    {
        String filename = "eventlibrary.atd";
        try
        {
            BufferedReader reader = new BufferedReader(new FileReader(new File("libraries" + S + filename)));
            String read = "";
            reader.readLine(); //disposes the first line
            do
            {
                try
                {
                    read = reader.readLine();
                    if(read != null)
                    {
                        String[] bits = read.split(",");
                        events.add(parseFromString(bits));
                    }
                }
                catch (Exception e)
                {
                }
            } while(read != null);
            reader.close();
        }
        catch (Exception e)
        {
        }
    }
    
    public static Event parseFromString(String[] parts)
    {
        SimpleDateFormat dateFormat = new SimpleDateFormat("dd MMM yyyy");
        Date date = dateFormat.parse(parts[1], new ParsePosition(0));
        if(parts[2].equals("FIELD MEET"))
        {
            FieldMeet fm = new FieldMeet(parts[0], date);
            boolean[] pf = new boolean[7];
            for(int i = 0; i < parts[5].length(); i++)
                pf[i] = Integer.parseInt(String.valueOf(parts[5].charAt(i))) == 1;
            fm.setPerformed(pf);
            return fm;
        }
        return new Event(parts[0], date, parts[2], parts[3], Integer.parseInt(parts[4]));
    }
}